﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class Frmaddschedule : Form
    {
        public Frmaddschedule()
        {
            InitializeComponent();
        }
        private string o_classid = null;
        private string o_termid = null;
        private string o_kcid = null;
        private string o_teaid = null;
        public Frmaddschedule(string termid,string classid,string kcid,string teaid)
        {
            InitializeComponent();
            this.o_termid = termid;
            this.o_classid = classid;
            this.o_kcid = kcid;
            this.o_teaid = teaid;
            try
            {
                DataSet dsClass =CoursescheduleOperation.getCourseschedule(new CoursescheduleData());
                this.combClassid.DataSource = dsClass.Tables[0];
                this.combClassid.ValueMember = "班级名称";
                this.combClassid.DisplayMember = "班级名称";
                DataSet dsTea = TeacherInfoOperation.getTeacherInfo(new TeacherInfoData());
                this.combTeaid.DataSource = dsTea.Tables[0];
                this.combTeaid.ValueMember = "教师编号";
                this.combTeaid.DisplayMember = "教师姓名";
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
            if(o_termid!=""&&o_classid!=""&&o_kcid!="")
            {
               CoursescheduleData data=new CoursescheduleData ();
                data.Termid = termid; data.Classid = classid;
                data.Kcid = kcid;
                DataSet ds = CoursescheduleOperation.getCourseschedule(data);
                this.txtTermid.Text = ds.Tables[0].Rows[0]["学期"].ToString();
                this.combClassid.Text = ds.Tables[0].Rows[0]["班级名称"].ToString();
                this.txtKcid.Text = ds.Tables[0].Rows[0]["课程编号"].ToString();
                this.Txtcname.Text = ds.Tables[0].Rows[0]["课程名称"].ToString();
                this.combTeaid.Text = ds.Tables[0].Rows[0]["教师姓名"].ToString();
                this.txtTermid.Enabled = false;
                this.combClassid.Enabled = false;
                this.txtKcid.Enabled = false;
                this.Txtcname.Enabled = false;
                this.Text = "课程表修改";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string termid = this.txtTermid.Text;
            string classid = (string)this.combClassid.SelectedValue;
            string kcid = this.txtKcid.Text;
            string teaid = (string)this.combTeaid.SelectedValue;
            if (termid == null || termid.Trim().Equals(""))
            {//学期不允许为空
                MessageBox.Show("请输入学期！","提示");
                this.txtTermid.Focus();
                return;
            }
            if(classid==null&&!classid.Trim().Equals(""))
            {//必须选择班级名称
                MessageBox.Show("请选择班级！","提示");
                this.combClassid.Focus(); return;
            }
            if(kcid==null||kcid.Trim().Equals(""))
            {//课程编号不允许为空
                MessageBox.Show("请输入课程代号！","提示");
                this.txtKcid.Focus();
                return;
            }
            if(teaid==null||teaid.Trim().Equals(""))
            {//必须选择教师
                MessageBox.Show("请选择教师！","提示");
                this.combTeaid.Focus();
                return;
            }
            CoursescheduleData data = new CoursescheduleData();
            data.Termid = termid;
            data.Classid = classid;
            data.Kcid = kcid;
            data.Teaid = teaid;
            try
            {
                if (o_termid == "")
                {
                    if (CoursescheduleOperation.insertCourseschedule(data))
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.txtKcid.Text = "";
                        this.combTeaid.Text = "";
                        this.Txtcname.Text = "";
                        this.combTeaid.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("添加失败！", "错误");
                    }
                }
                else
                {
                    if (CoursescheduleOperation.updateCourseschedule(data))
                    {
                        MessageBox.Show("修改成功！", "提示");
                        this.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("修改失败！","错误");
                    }
                }
            }
            catch(Exception ex)
            {
                ex.ToString();
                MessageBox.Show("保存失败！","错误");
            }
        }

        private void txtKcid_TextChanged(object sender, EventArgs e)
        {//获取对应课程编号的课程名称
            if (this.txtKcid.Text.Trim() != "")
            {
                CourseInfoData data = new CourseInfoData();
                data.Kcid = this.txtKcid.Text;
                DataSet dsKc = CourseInfoOperation.getCourseInfo(data);
                if (dsKc.Tables[0].Rows.Count > 0)
                {
                    this.Txtcname.Text = dsKc.Tables[0].Rows[0]["课程名称"].ToString();
                }
            }
            else
            {
                this.Txtcname.Text = "";
            }
        }
    }
}